using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful;
using CodePlex.Resourceful.Amazon.S3;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class ServerAccessLoggingEditorForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {

        private S3Connection _s3Connection;
        private string _bucket;
        private string _selectedBucket;  

        public ServerAccessLoggingEditorForm()
        {
            InitializeComponent();

            this.MinimumSize = this.Size;

            _descriptionLabel.Text = Properties.Messages.ServerAccessLogging_Description;

            _isEnabledCheck.CheckedChanged += delegate
            {
                this.UpdateData();
                _saveButton.Enabled = true;
            };
            _targetBucketCombo.TextChanged += delegate
            {
                _saveButton.Enabled = true;
                _selectedBucket = _targetBucketCombo.Text;
            };
            _targetPrefixBox.TextChanged += delegate
            {
                _saveButton.Enabled = true;
            };

            this.SetupDialogWork(_cancelButton, _saveButton, delegate
            {
                bool isEnabled = _isEnabledCheck.Checked;
                string targetBucket = _selectedBucket;
                string targetPrefix = _targetPrefixBox.Text;

                BucketLoggingStatus status = isEnabled ? BucketLoggingStatus.Enabled(targetBucket, targetPrefix) : BucketLoggingStatus.Disabled;

                _s3Connection.SetLoggingStatus(_bucket, status);

                return true;
            });

        }

        public void Initialize(S3FileSystem s3fs, string bucket)
        {
            _s3Connection = s3fs.S3Connection;
            _bucket = bucket;

            this.Text = string.Format(Properties.Messages.ServerAccessLogging_Title, bucket);
      
            BucketLoggingStatus status = s3fs.S3Connection.GetLoggingStatus(bucket);


            object[] bucketNames = s3fs.S3Connection.ListBuckets().Buckets.ConvertAll<object>(delegate(Bucket item)
            {
               return item.Name;
            }).ToArray();

            _targetBucketCombo.Items.AddRange(bucketNames);

           _isEnabledCheck.Checked = status.IsEnabled;
           _targetBucketCombo.Text = status.TargetBucket;
           _targetPrefixBox.Text = status.TargetPrefix;

           _saveButton.Enabled = false;

           this.UpdateData();

        }

        private void UpdateData()
        {
            _targetBucketLabel.Visible = _isEnabledCheck.Checked;
            _targetBucketCombo.Visible = _isEnabledCheck.Checked;

            _targetPrefixLabel.Visible = _isEnabledCheck.Checked;
            _targetPrefixBox.Visible = _isEnabledCheck.Checked;
        }
      

    }
}

